<?php
namespace app\wxapp\service;
use app\common\model\Recommend;
use think\facade\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/4 0004
 * Time: 22:27
 */
class RecommendService{

    public function recommend($userId){
        $userService = new WechatUserService();
        $cateIds = $userService->getSelectedCategory($userId);
        //暂时只取这么多
        $recommends = Db::table("recommend")->whereIn("category_id",$cateIds)->limit(0,10)->select()->toArray();
        $videoIds = [];
        $topicIds = [];
        foreach($recommends as $key =>$val ){
            if($val['type'] == Recommend::TYPE_VIDEO){
                array_push($videoIds,$val['src_id']);
            }else if($val['type'] == Recommend::TYPE_TOPIC){
                array_push($topicIds,$val['src_id']);
            }
        }

        $videos = Db::table("video_list")->whereIn("id",$videoIds)->column("id,title,out_video_id,cover_url","id");
        $topics = Db::table("topic")->whereIn("id",$topicIds)->column("id,title,cover_url","id");


        foreach($recommends as $key => $val ){
            if($val['type'] == Recommend::TYPE_VIDEO){
                $recommends[$key]['data'] = isset($videos[$val['src_id']])?$videos[$val['src_id']]:[];
            }else if($val['type'] == Recommend::TYPE_TOPIC){
                $recommends[$key]['data'] = isset($topics[$val['src_id']])?$topics[$val['src_id']]:[];
            }
        }
        return $recommends;
    }

}